#include "groupsessiondetailwidget.h"
#include "debug.h"

GroupSessionDetailWidget::GroupSessionDetailWidget(QWidget *parent)
{
    // 1. 设置窗口的基本事件
    this->setFixedSize(410, 600);
    this->setWindowTitle("群聊详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QDialog { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2, 创建布局管理器
    QVBoxLayout *vlayout = new QVBoxLayout();
    vlayout->setSpacing(10);
    vlayout->setContentsMargins(50, 20, 50, 50);
    vlayout->setAlignment(Qt::AlignTop);
    this->setLayout(vlayout);

    // 3. 创建滚动区
    QScrollArea *scrollArea = new QScrollArea();
    scrollArea->setWidgetResizable(true);
    scrollArea->verticalScrollBar()->setStyleSheet(
        "QScrollBar:vertical { width: 2px; background-color: rgb(255, 255, 255); }");
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0; }");
    scrollArea->setFixedSize(310, 350);
    scrollArea->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    scrollArea->setStyleSheet("QWidget { background-color: transparent; border: none; }");

    // 创建一个 QScrollArea 内部的 QWidget
    QWidget *container = new QWidget();
    scrollArea->setWidget(container);

    // 给 container 里面添加一个网络布局
    _glayout = new QGridLayout();
    _glayout->setSpacing(10);
    _glayout->setContentsMargins(0, 0, 0, 0);
    _glayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    container->setLayout(_glayout);

    // 把滚动区添加到布局管理器中
    vlayout->addWidget(scrollArea);

    // 4. 添加 “添加按钮”
    AvatarItem *addBtn = new AvatarItem(QIcon(":/resource/image/cross.png"), "添加");
    _glayout->addWidget(addBtn, 0, 0);

    // 5. 添加 “群聊名称”
    QLabel *groupNameTag = new QLabel();
    groupNameTag->setText("群聊名称");
    groupNameTag->setStyleSheet("QLabel {font-weight: 700; font-size: 16px;}");
    groupNameTag->setFixedHeight(50);
    groupNameTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    // 设置文字在 QLabel 内部的对齐方式
    groupNameTag->setAlignment(Qt::AlignBottom);
    vlayout->addWidget(groupNameTag);

    // 6. 添加 真实群聊名称 和 修改按钮
    // 创建水平布局
    QHBoxLayout *hlayout = new QHBoxLayout();
    hlayout->setSpacing(0);
    hlayout->setContentsMargins(0, 0, 0, 0);
    vlayout->addLayout(hlayout);

    // 创建真实群聊名称的 QLabel
    _groupNameLabel = new QLabel();
    _groupNameLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    _groupNameLabel->setFixedHeight(50);
    _groupNameLabel->setStyleSheet("QLabel {font-size: 18px; }");
    hlayout->addWidget(_groupNameLabel, 0, Qt::AlignLeft | Qt::AlignVCenter);

    // 创建 修改按钮
    QPushButton *modifyBtn = new QPushButton();
    modifyBtn->setFixedSize(30, 30);
    modifyBtn->setIconSize(QSize(20, 20));
    modifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    modifyBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; } QPushButton:pressed { "
                             "background-color: rgb(230, 230, 230); }");
    hlayout->addWidget(modifyBtn, 0, Qt::AlignRight | Qt::AlignVCenter);

    // 7. 退出群聊按钮
    QPushButton *exitGroupBtn = new QPushButton();
    exitGroupBtn->setText("退出群聊");
    exitGroupBtn->setFixedHeight(50);
    exitGroupBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    QString btnStyle =
        "QPushButton { border: 1px solid rgb(90, 90, 90); border-radius: 5px; background-color: transparent;}";
    btnStyle += "QPushButton:pressed { background-color: rgb(230, 230, 230); }";
    exitGroupBtn->setStyleSheet(btnStyle);
    vlayout->addWidget(exitGroupBtn);

#if TEST_UI
    _groupNameLabel->setText("青龙学习小组");
    QIcon avatar(":/resource/image/defaultAvatar.png");
    for (int i = 0; i < 20; ++i)
    {
        AvatarItem *item = new AvatarItem(avatar, "张三" + QString::number(i));
        this->addMember(item);
    }
#endif
}

void GroupSessionDetailWidget::addMember(AvatarItem *avatarItem)
{
    const int MAX_COL = 4;
    if (_curCol >= MAX_COL)
    {
        // 换行操作
        _curRow++;
        _curCol = 0;
    }
    _glayout->addWidget(avatarItem, _curRow, _curCol);
    ++_curCol;
}
